// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/service/module/entitytag_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/entitytag"
	"gin-sns/app/model/constants"
	entitytagModel "gin-sns/app/model/module/entitytag"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var EntitytagService = newEntitytagService()

func newEntitytagService() *entitytagService {
	return &entitytagService{}
}

type entitytagService struct {
}

//根据主键查询数据
func (s *entitytagService) SelectRecordById(id int64) (*entitytagModel.Entity, error) {
	return entitytag.EntitytagDao.SelectById(id)
}

//
func (s *entitytagService) SelectRecordsIn(idarr []int64) ([]entitytagModel.Entity, error) {

	list, err := entitytag.EntitytagDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *entitytagService) DeleteRecordById(id int64) bool {
	return entitytag.EntitytagDao.DeleteById(id)
}

//批量删除数据记录
func (s *entitytagService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := entitytag.EntitytagDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *entitytagService) AddSave(req *entitytagModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity entitytagModel.Entity

	//entity.Id = req.Id
	entity.TagId = req.TagId
	entity.EntityType = req.EntityType
	entity.EntityId = req.EntityId
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := entitytag.EntitytagDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *entitytagService) EditSave(req *entitytagModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &entitytagModel.Entity{Id: req.Id}
	ok, err := entitytag.EntitytagDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.TagId = req.TagId
	entity.EntityType = req.EntityType
	entity.EntityId = req.EntityId
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return entitytag.EntitytagDao.Update(entity)
}

//根据条件查询数据
func (s *entitytagService) SelectListAll(params *entitytagModel.SelectPageReq) ([]entitytagModel.Entity, error) {
	return entitytag.EntitytagDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *entitytagService) SelectListByPage(params *entitytagModel.SelectPageReq) ([]entitytagModel.Entity, *page.Paging, error) {
	return entitytag.EntitytagDao.SelectListByPage(params)
}

// 导出excel
func (s *entitytagService) Export(param *entitytagModel.SelectPageReq) (string, error) {
	head := []string{"", "标签id", "归属实体类型", "归属实体id", "0未审核 1正常状态 2审核未通过 3删除", "创建时间"}
	col := []string{"id", "tag_id", "entity_type", "entity_id", "status", "create_time"}
	return entitytag.EntitytagDao.SelectListExport(param, head, col)
}

func (s *entitytagService) AddArticleTags(articleId int64, tagIds []int64) {

	if articleId <= 0 || len(tagIds) == 0 {
		return
	}

	var entity *entitytagModel.Entity
	for _, tagId := range tagIds {
		entity = &entitytagModel.Entity{
			EntityId:   articleId,
			EntityType: constants.EntityTypeArticle,
			TagId:      tagId,
			CreateTime: time.Now(),
		}
		entitytag.EntitytagDao.Insert(entity)
	}
}

func (s *entitytagService) DeleteArticleTags(articleId int64) {

	req := new(entitytagModel.SelectPageReq)
	req.EntityId = articleId
	req.EntityType = constants.EntityTypeArticle

	list, _ := s.SelectListAll(req)
	for _, entity := range list {
		entitytag.EntitytagDao.Delete(&entity)
	}

}

func (s *entitytagService) DeleteArticleTag(articleId, tagId int64) {
	entity := &entitytagModel.Entity{EntityId: articleId, EntityType: constants.EntityTypeArticle, TagId: tagId}
	entitytag.EntitytagDao.FindOne(entity)

	entitytag.EntitytagDao.Delete(entity)

}

func (s *entitytagService) FindByArticleId(articleId int64) []entitytagModel.Entity {
	req := new(entitytagModel.SelectPageReq)
	req.EntityId = articleId
	req.EntityType = constants.EntityTypeArticle

	list, _ := s.SelectListAll(req)

	return list
}
